<?php
require_once('SteamAPI.class.php');

$id = htmlspecialchars($_GET['id']);
$img_path = './../images/steam-api/';

if (empty($_GET['search'])) {
    $searching = false;
}
else {
    $searching = true;
}
if (empty($_GET['p']) || !is_numeric($_GET['p'])) {
    $searchPageNum = '1';
}
else {
    $searchPageNum = $_GET['p']; 
}
    
if (empty($_GET['fs'])) {
    $fullSearch = false;
}
else {
    $fullSearch = true;
    $searching = true;
}
if(empty($id)) {
	$id = 'm_o_n_d_a_y';
}
else {    
    $full = $_GET['full'];
    
    if(empty($full) || ($full == '0') || ($full == 'false')) {
    	$full = false;
    }
    
    else {
        $full = true;
    }
}

$steamProfile = new SteamAPI($id);
?>

<?php if ((!$fullSearch) && ($steamProfile->getFriendlyName() != '')): ?>
    <?php if ($searching): ?>
        <div class="steamprofile">
    <?php endif; ?>
        <div class="avatar" title="<?=$steamProfile->getFriendlyName() ?>">
            <a class="friendlyname <?=$steamProfile->onlineState() ?>" href="<?=$steamProfile->baseUrl() ?>" title="<?php echo "View " . $steamProfile->getFriendlyName() . "'s Profile"; ?>">
                <img class="<?=$steamProfile->onlineState() ?>" src="<?php if ($full): ?><?=$steamProfile->getAvatarMedium() ?><?php else: ?><?=$steamProfile->getAvatarSmall() ?><?php endif; ?>" alt="avatar" />
            </a>
        </div>
        <div class="profileinfo">
            <a class="friendlyname <?=$steamProfile->onlineState() ?>" href="<?=$steamProfile->baseUrl() ?>" title="<?php echo "View " . $steamProfile->getFriendlyName() . "'s Profile"; ?>"><span><?=$steamProfile->getFriendlyName() ?></span></a>
            <?php if ($full): ?>
                <img class="statusimg" src="<?=$img_path ?>status_<?=$steamProfile->onlineState() ?>.png" alt="User is <?=$steamProfile->onlineState() ?>" title="User is <?=$steamProfile->onlineState() ?>" /> 
            <?php endif; ?>
            <div class="status">
                <?php if (!$full): ?>
                    <span class="<?=$steamProfile->onlineState() ?>" title="<?=$steamProfile->getShortStateMessage() ?>"><?=$steamProfile->getOnlineState() ?></span>         
                <?php else: ?>           
                    <?php if ($steamProfile->getStateMessage() != ''): ?>
                        <span class="<?=$steamProfile->onlineState() ?>"><?php echo $steamProfile->getStateMessage(); ?></span> 
                    <?php else: ?>
                        <span class="<?=$steamProfile->onlineState() ?>"><?=$steamProfile->getOnlineState() ?></span>
                    <?php endif; ?>
                <?php endif; ?>
            </div>    
        </div>
        <div class="icons">    
            <a href="steam://friends/add/<?=$steamProfile->getSteamID64() ?>" title="Add <?=$steamProfile->getFriendlyName() ?> as a friend"><img src="<?=$img_path ?>befriend.png" alt="Befriend User" /></a>
            <?php if ($steamProfile->onlineState() != 'offline'): ?>
                <a href="steam://friends/message/<?=$steamProfile->getSteamID64() ?>" title="Send <?=$steamProfile->getFriendlyName() ?> a message"><img src="<?=$img_path ?>sendmsg.png" alt="Send Message" /></a>
            <?php endif; ?>        
        </div>
        <?php if($full): ?>
        <div id="profileSteamID" title="<?=$steamProfile->getSteamID() ?>"></div>        
        <?php endif; ?>
        <?php if ($searching): ?>
            <p id="SearchOutputError"></p>
        <?php endif; ?>
    <?php if ($searching): ?>
        </div>
    <?php endif; ?>
<?php elseif ($fullSearch): ?>
    <?php 
        function nodeContent($n, $outer = false) { 
            $d = new DOMDocument('1.0'); 
            $b = $d->importNode($n->cloneNode(true), true); 
            $d->appendChild($b); 
            $h = $d->saveHTML(); 
            // remove outter tags 
            if (!$outer) {
                $h = substr($h, (strpos($h, '>') + 1), -(strlen($n->nodeName) + 4));
            }
             
            return $h; 
        }
    
        $searchURL = 'http://steamcommunity.com/actions/Search?T=Account&K="' . urlencode($id) . '"&p=' . $searchPageNum;
    
        $html = @file_get_contents($searchURL);
    
        $dom = new DomDocument();
        @$dom->loadHTML($html);
        $finder = new DomXPath($dom); 
        $nodes = $finder->query("//div[contains(@class, 'resultItem')]/p/a[contains(@class, 'linkTitle')]/@href");     
        
        foreach($nodes as $node) {
            echo "<div class=\"searchResultItem\">";

            $playerID = end(explode('/', $node->nodeValue));
            $playerName = nodeContent($node->parentNode);
            
            echo nodeContent($node->parentNode->parentNode->parentNode);
                  
            echo "<div class=\"searchInfo\" title=\"{$playerID}\" style=\"display: none;\">{$playerName}</div>";
            echo "</div>";
        }
              
        if ($nodes->length == 0) {
            echo '<p id="SearchOutputError">No results found.  Please check your search parameters and try again!</p>';
        }
    ?>   
    
<?php endif; ?>
